package com.maoye.ossp.sys.news.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.hotent.org.api.model.User;
import com.hotent.org.api.service.UserService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.news.entity.NewsCategory;
import com.maoye.ossp.sys.news.entity.SelfNews;
import com.maoye.ossp.sys.news.service.INewsCategoryService;
import com.maoye.ossp.sys.news.service.ISelfNewsService;
import com.maoye.ossp.sys.org.util.UserUtils;

@Controller
@RequestMapping(value = "/sys/news/selfnews")
public class SelfNewsController extends BaseController{
	
	@Resource
	private ISelfNewsService IselfNewsService;
	@Resource
	private INewsCategoryService InewsCategoryService;
	@Resource
	private UserService userservice;
	/**
	 * 查询登陆人的所有新闻信息
	 * @param selfNews
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "/list")
	public String list(SelfNews selfNews, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String createBy = UserUtils.getCurrentUserId().toString();
		QueryFilter queryFilter = getQuerFilter(request,selfNews);
		if(createBy!=null||createBy!=""){
			queryFilter.addFilter("createBy", createBy, QueryOP.EQUAL);
		}
		Page<SelfNews> page = (Page<SelfNews>)IselfNewsService
				.find(queryFilter);
		model.addAttribute("page", page);
		return "/sys/news/selfnew/list";

	}
	
	/**
	 * 跳转到添加页面的方法
	 * 
	 * @param news
	 *            参数
	 * @param model
	 *            模型
	 * @return String
	 */
	@RequestMapping(value = "/add")
	public String add(SelfNews selfNews, Model model) {
		List<NewsCategory> list = InewsCategoryService.getAll();
		model.addAttribute("categoryList",list);
		return "/sys/news/selfnew/add";
	}
	
	/**
	 * 保存新闻信息
	 * @param selfNews
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/save")
	public String save(SelfNews selfNews,HttpServletRequest request
			,RedirectAttributes redirectAttributes,Model model){
		selfNews.setId(idGenerator.getUId());
		selfNews.setCreateBy(UserUtils.getCurrentUserId().toString());
		selfNews.setCreateTime(new Date());
		IselfNewsService.create(selfNews);
		addMessage(redirectAttributes, "保存新闻信息成功！");
		return "redirect:/sys/news/selfnews/list";
	}
	
	/**
	 * 修改新闻数据
	 * 
	 * @param id
	 *            根据主键修改(参数)
	 * @param model
	 *            模型
	 * @return String
	 */
	@RequestMapping(value = "/{id}/edit")
	public String edit(@PathVariable String id, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		SelfNews selfNews = IselfNewsService.get(id);
		User user = userservice.getById(UserUtils.getCurrentUserId().toString());
		selfNews.setUpdateBy(UserUtils.getCurrentUserId().toString());
		selfNews.setUpdateName(user.getFullname());
		selfNews.setUpdateTime(new Date());
		selfNews.setCreateBy(selfNews.getCreateBy());
		selfNews.setCreateTime(selfNews.getCreateTime());
		List<NewsCategory> list = InewsCategoryService.getAll();
		model.addAttribute("categoryList",list);
		model.addAttribute("selfNews", selfNews);
		return "/sys/news/selfnew/update";
	}
	
	/**
	 * 保存修改的数据
	 * @param selfNews
	 * @param request
	 * @param redirectAttributes
	 * @param model
	 *
	 */
	@RequestMapping(value = "/saveUpdate")
	public String saveUpdate(SelfNews selfNews,HttpServletRequest request
			,RedirectAttributes redirectAttributes,Model model){
		selfNews.setUpdateBy(UserUtils.getCurrentUserId().toString());
		selfNews.setUpdateTime(new Date());
		IselfNewsService.update(selfNews);
		addMessage(redirectAttributes, "修改新闻信息成功！");
		return "redirect:/sys/news/selfnews/list";
	}
	
	/**
	 * 根据ID查看新闻信息
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/view")
	public String view(@PathVariable String id,Model model){
		SelfNews selfNews = IselfNewsService.get(id);
		model.addAttribute("selfNews",selfNews);
		return "/sys/news/selfnew/view";
	}
	
	/**
	 * 删除新闻信息
	 * @param id
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * 
	 */
	@RequestMapping(value = "/{id}/delete")
	public String remove(@PathVariable String id,Model model
			,RedirectAttributes redirectAttributes){
		IselfNewsService.remove(id);
		addMessage(redirectAttributes, "删除新闻信息成功！");
		return "redirect:/sys/news/selfnews/list";
	}
	
	
}
